#include "Pyramid.h"

char* subString(char * str, int start, int n)
{
	char *buffer = new char[256];
	for(int i = 0;i<n;i++)
	{
		buffer[i] = str[i+start];
	}
	buffer[n] = '\0';
	return buffer;
}
vector<char*> split(char* str,char separator)
{
	vector<char*> strings;
	int length= strlen(str);
	int start=0;
	int n = 0;
	for(int i=0;i<length;i++)
	{
		
		if(str[i] == separator)
		{			
			strings.push_back(subString(str,start,n));
			n=0;
			start = i+1;
		}
		else
		{
			n++;
		}
	}
	strings.push_back(subString(str,start,n));
	return strings;
}

Pyramid::Pyramid(void)
{
}


Pyramid::~Pyramid(void)
{
}

bool Pyramid::contain(unsigned int cellState,unsigned int something)
{
	return ((cellState | something ) == cellState );
}
unsigned int Pyramid::addProperty(unsigned int &cellState,unsigned int something)
{
	cellState = cellState|something;
	return cellState;
}
bool Pyramid::containAt(int x,int y,unsigned int something)
{
	return contain(map[y][x],something);
}
int Pyramid::getNRow()
{
	return nRow;
}
int Pyramid::getNCol()
{
	return nCol;
}
void Pyramid::loadMap(char* filename)
{
	ifstream file(filename);
	file >> nRow;
	file >> nCol;
	map.resize(nRow);
	for(int i=0;i<nRow;i++)
	{
		map[i].resize(nCol);
		for(int j=0;j<nCol;j++)
		{
			char info[10];
			file >> info;
			vector<char*> listProperties = split(info,':');

			map[i][j] = NOTHING;
			for(int k=0;k<listProperties.size();k++)
			{
				if(strcmp(listProperties[k],"b")==0)
				{
					addProperty(map[i][j],BREEZY);
				}
				else if(strcmp(listProperties[k],"g")==0)
				{
					addProperty(map[i][j],GOLD);
				}
				else if(strcmp(listProperties[k],"p")==0)
				{
					addProperty(map[i][j],PIT);
				}
				else if(strcmp(listProperties[k],"s")==0)
				{
					addProperty(map[i][j],STENCH);
				}
				else if(strcmp(listProperties[k],"m")==0)
				{
					addProperty(map[i][j],MUMMY);
				}
			}
		}
	}
	file.close();
}



